/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.system.mapper.TrackingMerchantsPropertyMapper;
import org.springblade.modules.system.pojo.entity.TrackingMerchantsProperty;
import org.springblade.modules.system.service.ITrackingMerchantsPropertyService;
import org.springframework.stereotype.Service;

import java.util.Objects;


/**
 * 跟单宝商户核心配置表 服务实现类
 *
 * @author BladeX
 * @since 2020-12-27
 */
@Service
@Slf4j
public class TrackingMerchantsPropertyServiceImpl extends ServiceImpl<TrackingMerchantsPropertyMapper, TrackingMerchantsProperty> implements ITrackingMerchantsPropertyService {

	private static Logger logger = LoggerFactory.getLogger(TrackingMerchantsPropertyServiceImpl.class);

	private static final String TAG = "TrackingMerchantsPropertyServiceImpl";

	private static final String TENANT_ID = "188612";

	@Override
	public String getTenantIdByRequest(HttpServletRequest request) {
		String serverName = request.getServerName();
		int serverPort = request.getServerPort();
		StringBuilder builder = new StringBuilder();
		builder.append(serverName);
		if (serverPort != 80 && serverPort != 443) {
			builder.append(":").append(serverPort);
		}
		// 域名
		String domain = "http://" + builder.toString();
		log.info("{}-getTenantIdByRequest#domain:{}", TAG, domain);

		// 从域名配置表获取租户id
		TrackingMerchantsProperty trackingMerchantsProperty = this.getBaseMapper().selectOne(new LambdaQueryWrapper<TrackingMerchantsProperty>()
				.eq(TrackingMerchantsProperty::getSecondaryDistributionDomain, domain)
		);
		String tenantId = null;
		if (Objects.nonNull(trackingMerchantsProperty)) {
			tenantId = trackingMerchantsProperty.getTenantId();
			log.info("{}-getTenantIdByRequest#tenantId:{}", TAG, tenantId);
		}
		// todo 此判断逻辑上线时放开
//		if (StringUtil.isEmpty(tenantId)) {
//			throw new ServiceException("租户不存在!");
//		}
//		return tenantId;

		return TENANT_ID;
	}
}
